package com.lc.cehui.ui.activity.home;

import android.content.Intent;
import android.graphics.Paint;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.CompanyGoodsListApi;
import com.lc.cehui.http.response.CompanyGoodsListBean;
import com.lc.cehui.other.KeyboardWatcher;
import com.lc.cehui.ui.activity.GoodsDetailsActivity;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class AgencyActivity extends AppActivity  implements KeyboardWatcher.SoftKeyboardStateListener,
        TextView.OnEditorActionListener{
    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.ll_seach)
    LinearLayout llSeach;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;

    @BindView(R.id.check_jiage)
    CheckBox checkJiage;
    @BindView(R.id.check_xiaoliang)
    CheckBox checkXiaoliang;
    @BindView(R.id.recyclerView_choice)
    RecyclerView recyclerViewChoice;
    @BindView(R.id.et_text)
    EditText etText;

    private BaseQuickAdapter adapter;
    private int mPage=1;
    private int price=1;
    private int volume=1;
   private List<CompanyGoodsListBean.DataBean> dataList=new ArrayList<>();
    private String flag;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_agency;
    }

    @Override
    protected void initView() {
        titleBar.setTitle("资质代办");
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
        checkJiage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    price=2;

                }else {
                    price=1;
                }
                mPage=1;
                dataList.clear();
                company_goods_list(mPage,price,volume);
            }
        });
        checkXiaoliang.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    volume=2;

                }else {
                    volume=1;
                }
                mPage=1;
                dataList.clear();
                company_goods_list(mPage,price,volume);
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter = new BaseQuickAdapter<CompanyGoodsListBean.DataBean, BaseViewHolder>(R.layout.item_agency,dataList) {
            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, CompanyGoodsListBean.DataBean bean) {
                GlideApp.with(AgencyActivity.this).load(bean.getPicurl()).apply(new RequestOptions().placeholder(R.drawable.image11).error(R.drawable.image11)).into((ImageView) baseViewHolder.getView(R.id.img));
                baseViewHolder.setText(R.id.tv_title,bean.getGoods_name());
                baseViewHolder.setText(R.id.tv_company,bean.getName());
                baseViewHolder.setText(R.id.tv_preprice,"¥"+bean.getPrice());
                TextView price=baseViewHolder.getView(R.id.tv_price);
                price.setText("¥"+bean.getY_price());
                price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
                baseViewHolder.getView(R.id.tv_shopcar).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            }
        });
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                startActivity(new Intent(getContext(), GoodsDetailsActivity.class).putExtra("id",dataList.get(position).getId()));
            }
        });
        recyclerViewChoice.setLayoutManager(new GridLayoutManager(AgencyActivity.this, 4));
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                Log.e("TAG", "onRefresh:---- " );
                super.onRefresh(refreshLayout);
//                new Handler().postDelayed(() -> {

                mPage = 1;
                dataList.clear();
                company_goods_list(mPage,price,volume);
                refreshLayout.finishRefreshing();
//                }, 1000);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);



                if (flag.equals("T")) {
                    mPage++;


                    company_goods_list(mPage,price,volume);
                } else {

                }
                refreshLayout.finishLoadmore();

            }
        });
        showEmptyView(adapter);
        etText.setOnEditorActionListener(this);

    }

    @Override
    protected void initData() {
        dataList.clear();
        company_goods_list(mPage,price,volume);

    }

    private void company_goods_list(int page,int price,int volume) {
        EasyHttp.post(this)
                .api(new CompanyGoodsListApi().setType_pid(getIntent().getStringExtra("pid")).setType_id(getIntent().getStringExtra("id")).setPage(page).setPrice(price).setVolume(volume))
                .request(new HttpCallback<CompanyGoodsListBean>(this) {

                    @Override
                    public void onSucceed(CompanyGoodsListBean data) {

                        if (data.getCode().equals("200")) {
                            flag = data.getFlag();
                            dataList.addAll(data.getData());
                            adapter.setNewData(dataList);
                            adapter.notifyDataSetChanged();
                        }

                    }
                });
    }

    @OnClick({ R.id.ll_seach})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_seach:
                break;


        }
    }


    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            // 模拟点击登录按钮

            mPage = 1;
            dataList.clear();
            company_goods_list(mPage,price,volume);
            hideKeyboard(getCurrentFocus());
//            搜索


        }
        return  false;
    }


    @Override
    public void onSoftKeyboardOpened(int keyboardHeight) {

    }

    @Override
    public void onSoftKeyboardClosed() {

    }
}
